package com.fourWork.workFive;

import com.fourWork.ThreeTest;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ClientFile {
    //声明Socket对象
    Socket socket;
    //声明BufferedReader对象
    BufferedReader bufferedReader = null;
    //声明PrintStream对象
    PrintStream printStream = null;
    Scanner scanner = new Scanner(System.in);
    public ClientFile(Socket socket){
        this.socket=socket;
    }
    public static void main(String[] args) {

        try {
            Socket socket=new Socket("127.0.0.1", 8888);
            Thread thread=new SentFile(socket);
            Thread thread1=new ReceiveFile(socket);
            thread.start();
            thread1.start();
            thread.join();
            thread1.join();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
/*
    public void send(String str, int port) {
        try {
            socket = new Socket(str, port);    //创建Socket类型的对象并提供服务器的主机名和端口号
            System.out.println("连接服务器成功！");

            //使用输入输出流进行通信
            printStream = new PrintStream(socket.getOutputStream());

            while (true) {
                System.out.println("请输入要发送的数据内容：");
                String next = scanner.next();
                printStream.println(next);  //客户端向服务器发送字符串内容
                System.out.println("客户端发送数据内容成功");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printStream != null) {
                printStream.close();
            }
        }
    }
    */
 /*
    public void receive() {
        //接受服务器发来的字符串内容
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true) {
                String s = bufferedReader.readLine();//接受服务器发来的字符串内容并打印
                System.out.println("服务器回发的消息为：" +s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  */

}
class SentFile extends Thread{
    //声明Socket对象
    Socket socket;
    //声明PrintStream对象
    PrintStream printStream = null;
    Scanner scanner=new Scanner(System.in);
    String str;
    int port;
    public SentFile(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {

        try {
            //socket = new Socket(str, port);    //创建Socket类型的对象并提供服务器的主机名和端口号
            //socket = socket;    //创建Socket类型的对象并提供服务器的主机名和端口号
            System.out.println("连接服务器成功！");

            //使用输入输出流进行通信
            printStream = new PrintStream(socket.getOutputStream());

            while (true) {
                System.out.println("请输入要发送的数据内容：");
                String next = scanner.next();
                File file = new File(next);
                if(file.exists()){
                    FileReader fileReader=new FileReader(next);
                    if(file.isDirectory()){
                        file.createNewFile();
                    }
                    int i=0;
                    while ((i=fileReader.read())!=-1){
                        printStream.write(i);
                        //System.out.println((char)i);
                        printStream.flush();
                    }
                    printStream.println();
                    //System.out.println("向服务器发送文件地址："+next);

                }else {
                    System.out.println("文件不存在");
                    file.createNewFile();
                    System.out.println("文件创建成功");
                    break;
                }

                //printStream.println();  //客户端向服务器发送字符串内容
                System.out.println("客户端发送文件成功");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printStream != null) {
                printStream.close();
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

class ReceiveFile extends Thread{
    //声明Socket对象
    Socket socket;
    //声明BufferedReader对象
    BufferedReader bufferedReader = null;
    public ReceiveFile(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
            //接受服务器发来的字符串内容
            //System.out.println("进入接受run");
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true) {
                    String s = bufferedReader.readLine();//接受服务器发来的字符串内容并打印
                    //FileReader fileReader=new FileReader(bufferedReader.readLine());
                    int i = 0;
                    //while((i=fileReader.read())!=-1){
                    // System.out.println("服务器回发的消息为：" + i);
                    //}
                    //System.out.println(socket);
                    System.out.println("文件下载成功，下载后的文件保存于：" + s + ",该文件由" + socket.getInetAddress() + "客户端所发");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

}